//
//  ViewController.swift
//  SelfLoveTweet
//
//  Created by panda on 17/3/15.
//  Copyright © 2017年 panda. All rights reserved.
//

import UIKit
import Social

class ViewController: UIViewController {
    private var nameArr : [String]?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
//        view.addSubview(birthdayLbl);
        initData()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        createSubviewFrame()
    }
    
    //MARK: - 方法处理
    func wordText() {
        
    }
    
    func dateChanged(datePicker: UIDatePicker) {
        
    }
    
    func sliderChanged(slider: UISlider) {
        
    }
    
    func createSubviewFrame() {
        let X : CGFloat = 80.0
        nameTF.frame = CGRect(x: X, y: 80, width: 250, height: 30)
        genderSC.frame = CGRect(x: X, y: nameTF.frame.maxY + 20, width: 80, height: 15)
        birthdayLbl.frame = CGRect(x: X, y: genderSC.frame.maxY + 20, width: 250, height: 150)
        wordLbl.frame = CGRect(x: X, y: birthdayLbl.frame.maxY + 20, width: 250, height: 30)
        earningLbl.frame = CGRect(x: X, y: wordLbl.frame.maxY + 20, width: 250, height: 20)
        straightLbl.frame = CGRect(x: X, y: earningLbl.frame.maxY + 20, width: 40, height: 20)
        btn.frame = CGRect(origin: CGPoint(x: (375-80) / 2,y: straightLbl.frame.maxY + 40), size: CGSize(width: 80, height: 30))
        shareBtn.frame = CGRect(origin: CGPoint(x: (375-80) / 2,y: btn.frame.maxY + 40), size: CGSize(width: 80, height: 30))
    }
    
    func commitAction(btn: UIButton) {
        print(nameTF.text ?? String())
        print(genderSC.selectedSegmentIndex)
        print(wordLbl.text ?? String())
        print(earningLbl.value)
        print(straightLbl.isOn)
        
        let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
        let now = Date()
        let components = (gregorian as NSCalendar?)?.components(NSCalendar.Unit.year, from: birthdayLbl.date, to: now, options: [])
        let age:Int! = components?.year
        print(age)
    }
    
    func shareBtnAction(sender: UIButton) {
        /*
         if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter){
         let twitterController:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
         twitterController.setInitialText(tweet)
         self.present(twitterController, animated: true, completion: nil)
         } else {
         showAlert("Twitter Unavailable", message: "Please configure your twitter account on device", buttonTitle: "Ok")
         }
         */
        if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) {
            let twitter : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
            twitter.setInitialText("lalalalalallalahhhhhhhhh")
        }
    }

    //MARK: - 初始化
    func initData() {
        nameArr = ["Name", "Gender", "Birthday", "Word", "Earning", "Straight"];
        var Y = 100;
        
        for index in 0  ..< (nameArr?.count)! {
            Y = 100 + index*30;
            let lbl = UILabel()
            lbl.text = nameArr?[index]
            lbl.textAlignment = NSTextAlignment.center
            lbl.font = UIFont.systemFont(ofSize: 16)
            lbl.frame = CGRect(x: 0, y: Y, width: 80, height: 20);
//            view.addSubview(lbl);
            
        }
        
        view.addSubview(nameTF);
        view.addSubview(genderSC);
        view.addSubview(birthdayLbl);
        view.addSubview(wordLbl);
        view.addSubview(earningLbl);
        view.addSubview(straightLbl);
        view.addSubview(btn)
        view.addSubview(shareBtn)
    }
    
    //MARK: - 懒加载
    fileprivate lazy var titleLbl : UILabel = {
        let title = UILabel()//(frame: CGRect(x: 0, y: 64, width: 375, height: 40)
        title.text = "LoveTweet"
        title.font = UIFont.systemFont(ofSize: 32)
        title.textColor = UIColor.black;
        title.textAlignment = NSTextAlignment.center
        return title;
    }()

    fileprivate lazy var nameTF : UITextField = {
        let name = UITextField()//(frame: CGRect(x: 0, y: 64, width: 375, height: 40)
        name.font = UIFont.systemFont(ofSize: 16)
        name.textColor = UIColor.black;
//        name.backgroundColor = UIColor.gray
        name.tag = 1000
        name.layer.borderWidth = 0.5;
        name.layer.cornerRadius = 2
        return name;
    }()
    
    fileprivate lazy var genderSC : UISegmentedControl = {
        let name = UISegmentedControl.init(items: ["M", "FM"])
        
        return name;
    }()
    
    fileprivate lazy var birthdayLbl : UIDatePicker = {
        let name = UIDatePicker()//(frame: CGRect(x: 0, y: 64, width: 375, height: 40)
        name.datePickerMode = UIDatePickerMode.date
        name.setDate(Date(), animated: true)
        name.maximumDate = Date()
        name.minimumDate = Date(timeIntervalSince1970: 0) //参数0的意思是从1970开始,若还要往后,则负数
        name.locale = Locale(identifier: "zh_CN")   //默认为英文
        name.addTarget(self, action: #selector(self.dateChanged(datePicker:)), for: UIControlEvents.valueChanged)
        return name;
    }()
    
    fileprivate lazy var wordLbl : UITextField = {
        let name = UITextField()//(frame: CGRect(x: 0, y: 64, width: 375, height: 40)
        name.font = UIFont.systemFont(ofSize: 16)
        name.textColor = UIColor.black;
        name.delegate = self
        name.layer.borderWidth = 0.5;
        name.layer.cornerRadius = 2
        name.tag = 1001
        return name;
    }()
    
    fileprivate lazy var earningLbl : UISlider = {
        let name = UISlider()//(frame: CGRect(x: 0, y: 64, width: 375, height: 40)
        name.minimumValue = 50;
        name.maximumValue = 300;
        name.value = 100
        name.addTarget(self, action: #selector(sliderChanged(slider:)), for: UIControlEvents.valueChanged)
        return name;
    }()
    
    fileprivate lazy var straightLbl : UISwitch = {
        let name = UISwitch()//(frame: CGRect(x: 0, y: 64, width: 375, height: 40)
        name.isOn = false
        return name;
    }()
    
    fileprivate lazy var btn : UIButton = {
        let btn = UIButton(type: UIButtonType.custom)
        btn.setTitle("commit", for: UIControlState.normal)
        btn.backgroundColor = UIColor.gray
        btn.layer.cornerRadius = 2;
        btn.addTarget(self, action: #selector(commitAction(btn:)), for: UIControlEvents.touchUpInside)
        return btn;
    }()
    
    fileprivate lazy var shareBtn : UIButton = {
        let btn = UIButton(type: UIButtonType.custom)
        btn.setTitle("share", for: UIControlState.normal)
        btn.backgroundColor = UIColor.gray
        btn.layer.cornerRadius = 2;
        btn.addTarget(self, action: #selector(commitAction(btn:)), for: UIControlEvents.touchUpInside)
        return btn;
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        // 键盘收起
        view.endEditing(true)
    }

}

extension ViewController: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField.tag == 1001 {
            wordText()
        }
        
    }
    
    
}

